<?php
class GalleryController extends Controller
{
	public function filters()
	{
		return array(
	            'accessControl',
		);
	}
	
	public function accessRules()
	{
		return array(
		array("allow",
				'actions'=>array('index','view','delete','photo','deletephoto'),
				'users'=>array('@')
		),
		array('deny',
		                'users'=>array('*'),
		),
		);
	}
	//图片库首页+添加图片库
	public function actionIndex(){
		$error=null;
		$info=null;
		$maxsize=10;
		if(isset($_POST['MemberGallery'])){
			$size = MemberGallery::model()->count("member_id=".Yii::app()->user->id." order by id desc");
			if ($size<$maxsize){
			$post= new MemberGallery();
			$post->attributes = $_POST['MemberGallery'];
			$post->title = strip_tags($post->title);
			if(empty($post->title)){
				$error='创建失败，标题不能为空！';
			}else{
				$post->description = strip_tags($post->description);
				$post->member_id = Yii::app()->user->id;
				$info='新图片库创建成功！';				
				$post->save();				
			}
			}else{
				$error='每个用户限制最多'.$maxsize."个图片库！";
			}
		}
		$galleries = MemberGallery::model()->findAll("member_id=".Yii::app()->user->id." order by id desc");
		$photos = MemberPhoto::model()->findAll("member_id=".Yii::app()->user->id." order by id desc");
		$this->render("index",array(
								"galleries"=>$galleries,
								"photos"=>$photos,
								"maxsize"=>10,
								"error"=>$error,
								"info"=>$info,
		));
	}
	//删除图片库和图片
	public function actionDelete($id){
		$gallery = $this->loadByPK($id);
		if($gallery->member_id ==Yii::app()->user->id ){
			$photo_path = Yii::app()->basePath."/../images/gallery/";
			$photos = MemberPhoto::model()->findAll("member_gallery_id=".$gallery->id);
			foreach ($photos as $photo){
				$photo->delete();
				@unlink($photo_path.$id."_".$photo->id.".png");
			}
			$gallery->delete();
		}
	}
	//删除图片
	public function actionDeletephoto($id){
		$photo = $this->loadImage($id);
		if($photo->member_id ==Yii::app()->user->id ){
			$photo_path = Yii::app()->basePath."/../images/gallery/";
			$photo->delete();
			@unlink($photo_path.$photo->member_gallery_id."_".$id.".png");
		}
	}
	//查看图片
	public function actionView($id){
		$gallery = $this->loadByPK($id);
		$maxsize=20;
		$uploaded = 0;
		$message = array();
		
		if($gallery->member_id ==Yii::app()->user->id ){			
			if(!empty($_FILES)){
				foreach ($_FILES['image']['name'] as $i => $name) {
					if ($_FILES['image']['error'][$i] == 4) {
						continue;
					}
					if ($_FILES['image']['error'][$i] == 0) {
						if(!$this->isAllowedExtension($name)) {
							$message[] = "$name 是非法格式.";
							continue;
						}
						if ($_FILES['image']['size'][$i] > 1000000) {
							$message[] = "$name 文件过大.";
							continue;
						}
			
						if (MemberPhoto::model() -> count("member_gallery_id=$id")>=$maxsize){
							$message[]="每个图片库限制最多".$maxsize."张图片";
							break;
						}
						$photo = new MemberPhoto();
						$photo->member_gallery_id = $id;
						$photo->member_id = $gallery->member_id;
						$photo->save();
			
						$file = Yii::app()->basePath."/../images/gallery/{$id}_{$photo->id}.png";
						$image_file=CUploadedFile::getInstanceByName("image[$i]");
						$image_file->saveAs("$file");
						$image = new Image();
						$image->generate_image_resize($file, $file, 300, 600);
						$uploaded++;
					}
				}
			}

			if(isset($_POST['MemberGallery'])){
				$gallery->attributes = $_POST['MemberGallery'];
				$gallery->title = strip_tags($gallery->title);
				if(empty($gallery->title)){
					$message[]='编辑图片库失败，标题不能为空！';
				}else{
					$gallery->description = strip_tags($gallery->description);
					$gallery->member_id = Yii::app()->user->id;
					$gallery->save();
				}
			}

			$photos = MemberPhoto::model()->findAll("member_gallery_id=".$gallery->id);
			$this->render("view",array(
									"gallery"=>$gallery,
									"photos"=>$photos,
									"maxsize"=>$maxsize,
									"uploaded"=>$uploaded,
									"message"=>$message,
			));
		}else{
			$this->redirect(array("site/page","view"=>"access_deny"));
		}		
	}
	public function actionPhoto($id){
		$photo = $this->loadImage($id);
		$this->render("photo", array(
							"photo"=>$photo,
		));
	}
	private function loadByPK($pk){
		$model = MemberGallery::model()->findByPk($pk);
		if(isset($model)){
			return $model;
		}else{
			$this->redirect(array("site/page","view"=>"error"));
		}
	}
	private function isAllowedExtension($fileName) {
		$allowedExtensions = array("jpg", "png", "gif");
		return in_array(end(explode(".", $fileName)), $allowedExtensions);
	}
	private function loadImage($pk){
		$model = MemberPhoto::model()->findByPk($pk);
		if(isset($model)){
			return $model;
		}else{
			$this->redirect(array("site/page","view"=>"error"));
		}
	}
}
